#include "../HResolutionConfigResolver.h"

int HResolutionConfigResolver::m_nPrimaryScreenHeight = 0;
int HResolutionConfigResolver::m_nPrimaryScreenWidth = 0;

HResolutionConfigResolver::HResolutionConfigResolver(QObject *parent)
    :QObject(parent)
{
    
}

HResolutionConfigResolver::~HResolutionConfigResolver()
{

}

bool HResolutionConfigResolver::dealResolutionConfigInfo(QString strConfigInfoPath)
{

	QRect primaryScreenRect = QApplication::primaryScreen()->geometry();
	m_nPrimaryScreenHeight = primaryScreenRect.height();
	m_nPrimaryScreenWidth = primaryScreenRect.width();

    QFile file(strConfigInfoPath);
    if(!file.open(QIODevice::ReadOnly))
    {
        return false;
    }

    QDomDocument doc;
    if(!doc.setContent(file.readAll()))
    {
        file.close();
        return false;
    }
    if(doc.isNull())
    {
        file.close();
        return false;
    }

    QDomElement rootElement = doc.documentElement();
    QDomNodeList elementList = rootElement.childNodes();
    QDomElement ResultResolutionElement;
    if(elementList.count() > 0)
    {
        ResultResolutionElement = elementList.at(0).toElement();
    }


    for(int i = 0; i < elementList.count(); i ++)
    {
        QDomNode node = elementList.at(i);
        QDomElement element = node.toElement();

        QString maxResolutionWidth = element.attribute("maxResolutionWidth");
        QString maxResolutionHeight = element.attribute("maxResolutionHeight");
        QString minResolutionWidth = element.attribute("minResolutionWidth");
        QString minResolutionHeight = element.attribute("minResolutionHeight");

        int nMaxRW = maxResolutionWidth.toInt();
        int nMaxRH = maxResolutionHeight.toInt();
        int nMinRW = minResolutionWidth.toInt();
        int nMinRH = minResolutionHeight.toInt();

        if(m_nPrimaryScreenWidth < nMaxRW && m_nPrimaryScreenHeight < nMaxRH && m_nPrimaryScreenWidth >= nMinRW &&\
                m_nPrimaryScreenWidth >= nMinRH )
        {
            ResultResolutionElement = element;
            //g_strImageRelationPath =
            break;
        }
    }

    QDomNodeList resolutionChildNodes = ResultResolutionElement.childNodes();
    for(int rcNodeIndex = 0; rcNodeIndex < resolutionChildNodes.count(); rcNodeIndex ++)
    {
        QDomNode rcNode = resolutionChildNodes.at(rcNodeIndex);
        QDomElement rcElement = rcNode.toElement();
        if(rcElement.tagName() == "imagePath")
        {
            g_strImageRelationPath = rcElement.attribute("path");
        }
        else if(rcElement.tagName() == "dataValuePath")
        {
            QString strDataValuePath = rcElement.attribute("path");
            dealDataValue(QApplication::applicationDirPath() + strDataValuePath);
        }
    }

    file.close();
    return true;
}

bool HResolutionConfigResolver::dealDataValue(QString strPath)
{
    QFile file(strPath);
    if(!file.open(QIODevice::ReadOnly))
    {
        return false;
    }

    QDomDocument doc;
    if(!doc.setContent(file.readAll()))
    {
        file.close();
        return false;
    }
    if(doc.isNull())
    {
        file.close();
        return false;
    }

    QDomElement rootElement = doc.documentElement();
    QDomNodeList elementList = rootElement.childNodes();

    for(int i = 0; i < elementList.count(); i ++)
    {
        QDomNode node = elementList.at(i);
        QDomElement element = node.toElement();

        QString strName = element.attribute("name");
        QString strValue = element.attribute("value");
        QString strType = element.attribute("type ");

        if(strName == "g_nGridSpcing")//need optimized
        {
            g_nGridSpcing = strValue.toInt();
        }
        else if(strName == "g_nCircelPanelWidth")
        {
            g_nCircelPanelWidth = strValue.toInt();
        }
        else if(strName == "g_nCircelPanelCenterR")
        {
            g_nCircelPanelCenterR = strValue.toInt();
        }
        else if(strName == "g_nCircelPanelButtonOffset")
        {
            g_nCircelPanelButtonOffset = strValue.toInt();
        }
        else if(strName == "g_nCircelPanelCenterDiskR")
        {
            g_nCircelPanelCenterDiskR = strValue.toInt();
        }
        else if(strName == "g_nCircelPanelButtonDiskR")
        {
            g_nCircelPanelButtonDiskR = strValue.toInt();
        }
        else if(strName == "g_nCameraControlBtnWidth")
        {
            g_nCameraControlBtnWidth = strValue.toInt();
        }
        else if(strName == "g_nGridMarginL")
        {
            g_nGridMarginL = strValue.toInt();
        }
        else if(strName == "g_nGridMarginU")
        {
            g_nGridMarginU = strValue.toInt();
        }
        else if(strName == "g_nGridMarginR")
        {
            g_nGridMarginR = strValue.toInt();
        }
        else if(strName == "g_nGridMarginB")
        {
            g_nGridMarginB = strValue.toInt();
        }
    }
    file.close();
    return true;
}
